<script>
	/**
	 * vuex管理登陆状态，具体可以参考官方登陆模板示例
	 */
	import {
		mapMutations
	} from 'vuex';
	export default {
		methods: {
			...mapMutations(['login'])
		},
		globalData: {
			privacyContractName: '', //隐私协议名称 是接口生成的！
			showPrivacy: false,  //全局控制弹窗显隐
			cartCount: 0 // 购物车数量，用于自定义 tabbar 显示
		},
		onLaunch: function() {
			// #ifdef MP
			const that = this;
			wx.getPrivacySetting({
			   success(res) {
				if (res.needAuthorization) {
					that.globalData.privacyContractName = res.privacyContractName;
					that.globalData.showPrivacy = true;
				} else {
					that.globalData.showPrivacy = false;
				}
			   }
			});
			// #endif
			
			let userInfo = uni.getStorageSync('userInfo') || '';
			if(userInfo.id){
				//更新登陆状态
				uni.getStorage({
					key: 'userInfo',
					success: (res) => {
						this.login(res.data);
					}
				});
			}
			
			// 初始化购物车数量
			this.initCartCount();
		},
		onShow: function() {
			
		},
		onHide: function() {
			
		},
		methods: {
			// 初始化购物车数量
			initCartCount() {
				try {
					// 从 store 获取购物车数量
					if (this.$store && this.$store.state && this.$store.state.app) {
						this.globalData.cartCount = this.$store.state.app.cartCount || 0;
					}
				} catch (e) {
					console.log('初始化购物车数量失败', e);
				}
			},
			// 更新购物车数量
			updateCartCount(count) {
				this.globalData.cartCount = count;
				// 同时更新 store
				if (this.$store) {
					this.$store.commit('app/UPDATE_CART_COUNT', count);
				}
			}
		},
	}
</script>

<style lang='scss'>
	@import "@/static/alifont/iconfont.css";
	
	view,
	scroll-view,
	swiper,
	swiper-item,
	cover-view,
	cover-image,
	icon,
	text,
	rich-text,
	progress,
	button,
	checkbox,
	form,
	input,
	label,
	radio,
	slider,
	switch,
	textarea,
	navigator,
	audio,
	camera,
	image,
	video {
		box-sizing: border-box;
	}
	/* 骨架屏替代方案 */
	.Skeleton {
		background: #f3f3f3;
		padding: 20upx 0;
		border-radius: 8upx;
	}

	/* 图片载入替代方案 */
	.image-wrapper {
		font-size: 0;
		background: #f3f3f3;
		border-radius: 4px;

		image {
			width: 100%;
			height: 100%;
			transition: .6s;
			opacity: 0;

			&.loaded {
				opacity: 1;
			}
		}
	}

	.clamp {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		display: block;
	}

	.common-hover {
		background: #f5f5f5;
	}

	/*边框*/
	.b-b:after,
	.b-t:after {
		position: absolute;
		z-index: 3;
		left: 0;
		right: 0;
		height: 0;
		content: '';
		transform: scaleY(.5);
		border-bottom: 1px solid $border-color-base;
	}

	.b-b:after {
		bottom: 0;
	}

	.b-t:after {
		top: 0;
	}

	/* button样式改写 */
	uni-button,
	button {
		height: 80upx;
		line-height: 80upx;
		font-size: $font-lg + 2upx;
		font-weight: normal;

		&.no-border:before,
		&.no-border:after {
			border: 0;
		}
	}

	uni-button[type=default],
	button[type=default] {
		color: $font-color-dark;
	}

	/* input 样式 */
	.input-placeholder {
		color: #999999;
	}

	.placeholder {
		color: #999999;
	}
	uni-page-wrapper {
		background: #f5f5f5;
	}
	.base-style {
		width: calc(100% - 40rpx);
		margin: 0 auto;
		margin-top: 20rpx;
		background: #fff;
		border-radius: 10rpx;
	}
	
	/* 移除 uni-app 默认 tabBar 边框 */
	.uni-tabbar-border {
		background-color: transparent !important;
	}
</style>
